import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';
import '../model/cabinet_info_model.dart';
import '../model/cabinet_info_status_model.dart';
import '../model/cabinet_list_model.dart';

class CabinetApi {
  /*
  * 获取列表
  * */
  static Future<List<CabinetListModel>> cabinetList(int page) async {
    try {
      Map<String, dynamic> params = {
        "Page": page,
        "PageSize": 10,
      };
      List<Map<String, dynamic>> list = await MGHttp.get("api/services/app/Branch/GetMyDeviceListByBranch", params: params);
      List<CabinetListModel> listModel = list.map((e) => CabinetListModel.fromJson(e)).toList();
      return listModel;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 设备信息
  * */
  static Future<List<CabinetInfoModel>> cabinetInfo(int deviceId) async {
    try {
      Map<String, dynamic> params = {
        "deviceId": deviceId,
      };
      List<Map<String, dynamic>> list = await MGHttp.get("api/services/app/Devices/GetLockBoardList", params: params);
      List<CabinetInfoModel> listModel = list.map((e) => CabinetInfoModel.fromJson(e)).toList();
      return listModel;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 状态
  * */
  static Future<List<CabinetInfoStatusModel>> cabinetStatus(int deviceId) async {
    try {
      Map<String, dynamic> params = {
        "deviceId": deviceId,
      };
      List<Map<String, dynamic>> list = await MGHttp.get("api/services/app/Devices/GetDeviceBindCourierSettings", params: params);
      List<CabinetInfoStatusModel> listModel = list.map((e) => CabinetInfoStatusModel.fromJson(e)).toList();
      return listModel;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 创建
  * */
  static Future<bool> createCabinetBind({int? boardNo, String? displayNo, String? courierPhone, bool? isActive}) async {
    try {
      Map<String, dynamic> params = {
        "boardNo": boardNo,
        "displayNo": displayNo,
        "courierPhone": courierPhone,
        "isActive": isActive,
      };
      bool result = await MGHttp.post(
        "api/services/app/Devices/CreateNewDeviceBindCourierSetting",
        params: params,
        progress: "创建中...",
      );
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 修改
  * */
  static changeBind({int? id, String? courierPhone, bool? isActive}) async {
    try {
      Map<String, dynamic> params = {
        "id": id,
        "courierPhone": courierPhone,
        "isActive": isActive,
      };
      bool result = await MGHttp.put(
        "api/services/app/Devices/UpdateDeviceBindCourierSetting",
        params: params,
        progress: "修改中..",
      );
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
